var Game = Class
		.extend({
			init : function(name) {
				this.FPS = 25;
				this.name = name;
				this.currentWeaponIndex = 0;
				this.radius = 12.5;
				this.mobsForLevelMax =24;
				this.mobsKilled = 0;
				this.playerHealth = 100;
				this.spawnRate = 2000;
				this.currentLevel = 0;
				this.levels = [
						new Level("LEVEL 1", imp, imp, level1Load, level1Back),
						new Level("LEVEL 2", demon, imp, level2Load, level2Back),
						new Level("LEVEL 3", baron, demon, level3Load, level3Back),
						new Level("HELL", cyborg, cyborg, level4Load,level4Back) 
						];

			},

			levelStart : function(level) {
				$(document).on("mousemove", 
						function(event) {
							x = event.pageX - (window.innerWidth - 1000) / 2;
							y = event.pageY - 35;
				});

				$(document).on("mousedown", function() {
					if (!player.shoping) {
						clearInterval(intervalNonShoot);
						interval = setInterval(callShooting, 40);
					}
				});
				$(document).on("mouseup",
						function() {
							if (!player.shoping) {
								clearInterval(interval);
								intervalNonShoot = setInterval(
										callNonShooting, 40);
							}

				});

				$(document).on("keypress",
				        function(event) {
							console.log(event.keyCode);
							var keyCode = event.keyCode
									|| event.charCode; // X-Browser
							// fix for
							// firefox
							if (keyCode == 114) {
								if (player.weapons[player.currentWeapon].reloading == false
										&& player.weapons[player.currentWeapon].currentBullets < player.weapons[player.currentWeapon].maxBulletsInMagazine
										&& player.weapons[player.currentWeapon].extraMagazines > 0){
										console.log(player.weapons[player.currentWeapon].extraMagazines);
								player.weapons[player.currentWeapon].reloading = true;
								player.weapons[player.currentWeapon].extraMagazines --;
								if (!gameOver){
								clipOut.play();}
								}
							}
							if (keyCode == 97) {
								player.weapons[player.currentWeapon]
										.aim();
							}
							if (keyCode == 49) {
								player.currentWeapon = 0;
							}
							if (keyCode == 50) {
								if (player.weapons.length > 1) {
									player.currentWeapon = 1;
								}
							}
							if (keyCode == 51) {
								if (player.weapons.length > 2) {
									player.currentWeapon = 2;
								}
							}
							if (keyCode == 52) {
								if (player.weapons.length > 3) {
									player.currentWeapon = 3;
								}
							}

				});
				if (a.currentLevel == 0) {
					ctx = document.getElementById("canvas").getContext("2d");
				}

				ctx.beginPath();
				ctx.drawImage(a.levels[a.currentLevel].loadingScreen, 0, 0,
						1000, 700);
				ctx.font = "40px Impact";
				ctx.fillStyle = "red";
				ctx.fillText(a.levels[a.currentLevel].number, 450,
						350);
				setTimeout(function() {
					levelID = requestAnimationFrame(a.levelDraw)
					spawnInterval = window.setInterval(a.spawn(
							a.levels[a.currentLevel].mob1,
							a.levels[a.currentLevel].mob2), 1000 / a.FPS);
				}, 4000)

			},
			levelDraw : function() {
				ctx.restore();
				console.log("2");

				ctx.clearRect(0, 0, 2000, 2000);
				player.weapons[player.currentWeapon].drawScope();

				ctx.drawImage(a.levels[a.currentLevel].font, 0, 0, 1000, 700);
				ctx.fillStyle = 'red';

				

				for (var i = monsters.length - 1; i >= 0; i--) {
					if (monsters[i].yCoord >= 350 && monsters[i].alive == true) { // damage
						// player
						a.playerHealth -= 0.1;
						monsters[i].atacking = true;
						if (a.playerHealth <= 0) {
							window.highScore = new HighScore();
							cancelAnimationFrame(levelID);
							clearInterval(spawnInterval);
							$("#canvas").remove();
							gameOver = true;
							monsters = [];
							if (window.highScore.isHighScore(player.points))
								console.log("vlizam")
								context = document.getElementById(
										"canvasHighscore").getContext("2d");
								context.beginPath();
								context.fillStyle = "red";
								context.font = "100px Defused";
								context.fillText("Game",550,200)
								context.fillText("Over",560,300)
							var b = $("<div/>").addClass('nameContainer');
							var c = $("<input/>").addClass('PlayerNameInput')
									.attr("placeholder", "Enter your name");
							$(b).appendTo("body").append(c);
							$("<button/>").addClass("btn").html("Submit Score")
									.appendTo(b).click(
											function() {

												window.highScore.setHighScore(
														player.points, $(c)
																.val());
											});

						}
					}
					if (!gameOver)
						monsters[i].draw(i);

				}

				ctx.fillStyle = 'green';
				ctx.fillText( '$:'+ player.money , 830, 50);
				
				ctx.fillStyle = "white";
				ctx.font = "12px arial";
				ctx.fillText('Points :' + player.points, 500, 30);
				player.weapons[player.currentWeapon].draw(player.currentWeapon);
				player.weapons[player.currentWeapon].drawScopeLines();
				if (player.damageMultiplicator == 2) {
					ctx.fillStyle = 'blue';
					ctx.font = "30 impact";
					
					ctx.fillText('DOUBLE DAMAGE !!!!', 360, 100);
				
				}
				ctx.fillStyle = "red" ;
				ctx.font="30px Defused";	
				ctx.strokeStyle = "white";
				ctx.fillText("+",50,670);
				
				ctx.fillStyle = "white";
				ctx.fillText(Math.round(a.playerHealth) ,80,670);
			

				levelID = requestAnimationFrame(a.levelDraw);
			},
			spawn : function(mob1, mob2) {
				a.spawnInterval1 = setInterval(function() {
					var spawned = new a.levels[a.currentLevel].mob1(Math
							.floor(Math.random() * 750), 330);
					monsters.push(spawned);
					this.mobsForNow++;
				}, 5000);
				a.spawnInterval2 = setInterval(function() {
					var spawned = new a.levels[a.currentLevel].mob2(Math
							.floor(Math.random() * 750), 330);
					monsters.push(spawned);

				}, 5000);
				a.ddSpawnInterval = setInterval(function() {
					var spawned = new DoubleDamage(1, 200);
					monsters.push(spawned);
				}, 25000);

			},
			Shoping : function() {
				cancelAnimationFrame(levelID);

				clearInterval(interval);
				clearInterval(a.spawnInterval1);
				clearInterval(a.spawnInterval2);
				clearInterval(a.ddSpawnInterval);
				$("body").find("*").off();
				ctx.clearRect(0, 0, 2000, 2000);
				if (a.currentLevel == 0) {
					p = new Shop();
				}

				p.initShop();

			},
			
		});
function callShooting() {
	weapon.Shooting();
	console.log("cat");
}
function callNonShooting() {
	weapon.nonShooting();
}
